package cn.amossun.starter.common.util;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: starter-parent
 * @description:
 * @author: Amos.Sun
 * @DateTime: 2020/03/19 19:49
 **/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Page<T> {

    private int size = 15;

    private int current = 1;

    private List<T> records;

    private int total;

    private int pages;

    public List<T> computeRecords(List<T> allRecords) {
        if(ObjectUtil.isEmpty(allRecords)) {
            return new ArrayList<>(0);
        }
        this.total = allRecords.size();
        this.records = ListUtil.page(current - 1, size, allRecords);
        return records;
    }

    public int getPages() {
        if (getSize() == 0 || getTotal() == 0 ) {
            return 0;
        }
        int pages = getTotal() / getSize();
        if (getTotal() % getSize() != 0) {
            pages++;
        }
        return pages;
    }
}
